package com.itheima.prince.smartpeking.impl.NewsCenterPagers;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.itheima.prince.smartpeking.R;
import com.itheima.prince.smartpeking.activity.MainActivity;
import com.itheima.prince.smartpeking.base.BaseMenuPager;
import com.itheima.prince.smartpeking.bean.NewsCenterBean;
import com.itheima.prince.smartpeking.impl.NewsCenterPagers.NewsCenterDetailBase.NewCenDeBase;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.viewpagerindicator.TabPageIndicator;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/25 0025.
 */
public class NewsCenterDetail extends BaseMenuPager {

    private List<NewsCenterBean.DataBean.ChildrenBean> mChildbean;
    private TabPageIndicator tabPageIndicator;
    private ViewPager viewPager;
    private List<BaseMenuPager> ncdBpagers;


    public NewsCenterDetail(Context context, List<NewsCenterBean.DataBean.ChildrenBean> children) {
        super(context);
        this.mChildbean = children;
    }

    @Override
    protected View initview() {
        View view = View.inflate(mcontext, R.layout.pager_newsdetail, null);
        //顶部指示器  需要关联viewpager
        tabPageIndicator = (TabPageIndicator) view.findViewById(R.id.tpi_indicator);
        viewPager = (ViewPager) view.findViewById(R.id.vp_newsdetail);
        return  view;
    }

    @Override
    public void initData() {
        ncdBpagers = new ArrayList<>();
        for (int i = 0; i <mChildbean.size() ; i++) {
            ncdBpagers.add(new NewCenDeBase(mcontext,mChildbean.get(i)));
        }
        MyPagerAdapter mPagerAdapter = new MyPagerAdapter();
        viewPager.setAdapter(mPagerAdapter);
        tabPageIndicator.setViewPager(viewPager);

        ncdBpagers.get(0).initData();
        //viewPager不能再设置了监听了，因为indicator已经给他设置过了，闲在只能监听indicator
        tabPageIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            private SlidingMenu slidingMenu;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                ncdBpagers.get(position).initData();
                MainActivity mainActivity = (MainActivity) mcontext;
                SlidingMenu slidingMenu =  mainActivity.getSlidingMenu();
                if(position==0){
                    slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                }else {
                    slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    class MyPagerAdapter extends PagerAdapter{

        @Override
        public CharSequence getPageTitle(int position) {
            return mChildbean.get(position).getTitle();
        }

        @Override
        public int getCount() {
            if (mChildbean==null){
                return 0;
            }
            return mChildbean.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View rootView = ncdBpagers.get(position).rootView;
            container.addView(rootView);

            return rootView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
